package com.ymatou.iapi.trading.operate.testcase;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.experimental.categories.Category;

import com.ymatou.iapi.trading.operate.parameter.Order;
import com.ymatou.iapi.trading.operate.parameter.PlaceOrderResp;
import com.ymatou.iapi.trading.operate.parameter.PlaceOrderService;
import com.ymatou.iapi.trading.operate.parameter.req.ApplySalesRefundRequestBean;
import com.ymatou.iapi.trading.operate.parameter.req.ChangeSalesRefundInfoRequestBean;
import com.ymatou.iapi.trading.operate.service.ChangeSalesRefundInfoRequestCall;
import com.ymt.core.tool.Logger;
import com.ymt.utils.annotation.TestCase;
import com.ymt.utils.annotation.TestSuite;
import com.ymt.utils.tag.P1;
import com.ymt.utils.tag.P3;
import com.ymttest.business.service.OrderCallService;
import com.ymttest.database.model.YmtRefundBill;
import com.ymttest.database.model.YmtRefundProduct;
import com.ymttest.database.sqlwapper.YmtRefundWapper;
import com.ymttest.utils.DataUtil;
import com.ymttest.utils.constant.ECode;

/**
 * 更新退货退款信息接口 FreeMaker Template自动生成代码
 */
@TestSuite
public class Ts_ChangeSalesRefundInfoRequest {
	private static ChangeSalesRefundInfoRequestBean changesalesrefundinforequestBean;
	private static ChangeSalesRefundInfoRequestCall changesalesrefundinforequestCall;

	@BeforeClass
	public static void setUp() {
		Logger.createResultFile("更新退货退款信息接口");
	}

	@Before
	public void caseUp() {
		changesalesrefundinforequestBean = new ChangeSalesRefundInfoRequestBean();
		changesalesrefundinforequestCall = new ChangeSalesRefundInfoRequestCall();
	}

	@AfterClass
	public static void tearDown() {
		Logger.generateResult(changesalesrefundinforequestCall.getOpurl());
	}

	@After
	public void caseDown() {
		Logger.end();
	}

	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_ChangeSalesRefundInfoRequest_001() {

		Logger.start(true, "更新退货退款信息成功");
		try {

			PlaceOrderResp placeOrderResp = PlaceOrderService.placeOrder7();

			int orderId = placeOrderResp.getOrderList().get(0).getOrderId();
			int sellerId = placeOrderResp.getOrderList().get(0).getSellerId();
			int userId = placeOrderResp.getUserId();
			int tradingId = placeOrderResp.getTradingId();

			Order order = placeOrderResp.getOrderList().get(0);

			BigDecimal orderFreight = order.getFreight();

			BigDecimal totalPrice = placeOrderResp.getPayAmount();// 支付金额

			OrderCallService orderCallService = new OrderCallService();

			// 付款
			orderCallService.orderPayRequest(userId, totalPrice, tradingId);

			// 申请退款
			String refundBillNo = orderCallService
					.applySalesRefundRequestInJava(orderId)
					.getSalesRefundInfoList().get(0).getRefundBillNo();

			BigDecimal oriRefund = orderCallService
					.applySalesRefundRequestInJava(orderId)
					.getSalesRefundInfoList().get(0).getRefundAmount();

			changesalesrefundinforequestBean.setOrderId(orderId);
			changesalesrefundinforequestBean.setRefundBillNo(refundBillNo);
			changesalesrefundinforequestBean.setRefundAmount(orderFreight
					.add(oriRefund));

			changesalesrefundinforequestBean.setRefundProductNum(order
					.getOrderInfoList().get(0).getAmount());
			changesalesrefundinforequestBean.setRefundType(1);
			changesalesrefundinforequestBean.setReason("123456789");
			changesalesrefundinforequestCall
					.setData(changesalesrefundinforequestBean);
			changesalesrefundinforequestCall.callService();

			Logger.verifyEquals(ECode.SUCCESS,
					changesalesrefundinforequestCall.getString("code"),
					"验证更新退货退款接口返回正常");

			DataUtil.verifyDecimalEquals(orderFreight, BigDecimal
					.valueOf((Double) changesalesrefundinforequestCall
							.getData().get("refundFreight")), "验证refundFreight");

			if (changesalesrefundinforequestCall.getString("code")
					.equals("200")) {

				// 数据库验证
				YmtRefundWapper ymtRefund = new YmtRefundWapper();
				Map m = new HashMap();
				m.put("orderId", orderId);
				m.put("refundBillNo", refundBillNo);

				YmtRefundBill refundBill = ymtRefund.selectRefundBill(m).get(0);
				YmtRefundProduct refundPro = ymtRefund.selectRefundProduct(m)
						.get(0);
				Logger.comment("校验Ymt_RefundBill表");
				Logger.verifyEquals(orderId, refundBill.getOrderId(),
						"验证OrderId");
				Logger.verifyEquals(refundBillNo, refundBill.getRefundBillNo(),
						"验证RefundBillNo");
				Logger.verifyEquals(
						changesalesrefundinforequestBean.getRefundAmount(),
						refundBill.getRefundAmount(), "验证RefundAmount");
				Logger.verifyEquals(0, refundBill.getRefundStatus(),
						"验证RefundStatus");
				Logger.verifyEquals(
						changesalesrefundinforequestBean.getRefundAmount(),
						refundBill.getSettlementAmount(), "验证SettlementAmount");
				Logger.verifyEquals(1, refundBill.getSalesRefundStatus(),
						"验证SettlementStatus");
				Logger.verifyEquals(
						changesalesrefundinforequestBean.getRefundType(),
						refundBill.getRefundType(), "验证RefundType");

				Logger.verifyEquals(sellerId, refundBill.getSellerId(),
						"验证SellerId");
				Logger.verifyEquals(userId, refundBill.getBuyerId(),
						"验证BuyerId");
				Logger.verifyEquals(orderId, refundBill.getOrderId(),
						"验证OrderId");
				DataUtil.verifyDecimalEquals(orderFreight,
						refundBill.getRefundedAmountOfFreight(),
						"验证RefundedAmountOfFreight");

				Logger.comment("校验Ymt_RefundProduct表");
				Logger.verifyEquals(orderId, refundPro.getOrderId(),
						"验证OrderId");
				Logger.verifyEquals(refundBillNo, refundPro.getRefundBillNo(),
						"验证RefundBillNo");
				Logger.verifyEquals(
						changesalesrefundinforequestBean.getRefundProductNum(),
						refundPro.getRefundProductNum(), "验证RefundProductNum");
			}

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_ChangeSalesRefundInfoRequest_002() {
		Logger.start(false, "订单号不存在");
		try {
			changesalesrefundinforequestBean.setOrderId(111111);
			changesalesrefundinforequestBean.setRefundBillNo("1233");
			changesalesrefundinforequestBean
					.setRefundAmount(new BigDecimal(100));
			changesalesrefundinforequestBean.setRefundProductNum(100);
			changesalesrefundinforequestBean.setRefundType(1);
			changesalesrefundinforequestBean.setReason("123456789");
			changesalesrefundinforequestCall
					.setData(changesalesrefundinforequestBean);
			changesalesrefundinforequestCall.callService();
			Logger.verifyEquals(ECode.FAILED201,
					changesalesrefundinforequestCall.getString("code"),
					"验证更新退货退款接口Code");
			Logger.verifyEquals(ECode.BCODE6,
					changesalesrefundinforequestCall.getString("bcode"),
					"验证更新退货退款接口BCode");
			Logger.verifyEquals(
					"订单不存在:" + changesalesrefundinforequestBean.getOrderId(),
					changesalesrefundinforequestCall.getString("msg"),
					"验证更新退货退款接口Msg");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_ChangeSalesRefundInfoRequest_003() {
		Logger.start(false, "退款单号不存在");
		try {
			PlaceOrderResp placeOrderResp = PlaceOrderService.placeOrder4();

			int orderId = placeOrderResp.getOrderList().get(0).getOrderId();
			int sellerId = placeOrderResp.getOrderList().get(0).getSellerId();
			int userId = placeOrderResp.getUserId();
			int tradingId = placeOrderResp.getTradingId();

			BigDecimal totalPrice = placeOrderResp.getPayAmount();// 支付金额

			OrderCallService orderCallService = new OrderCallService();
			// 申请退款
			String refundBillNo = orderCallService
					.applySalesRefundRequestInJava(orderId)
					.getSalesRefundInfoList().get(0).getRefundBillNo();
			changesalesrefundinforequestBean.setOrderId(orderId);
			changesalesrefundinforequestBean.setRefundBillNo("1111111");
			changesalesrefundinforequestBean.setRefundAmount(new BigDecimal(1));
			changesalesrefundinforequestBean.setRefundProductNum(100);
			changesalesrefundinforequestBean.setRefundType(1);
			changesalesrefundinforequestBean.setReason("123456789");
			changesalesrefundinforequestCall
					.setData(changesalesrefundinforequestBean);
			changesalesrefundinforequestCall.callService();
			Logger.verifyEquals(ECode.FAILED201,
					changesalesrefundinforequestCall.getString("code"),
					"验证更新退货退款接口Code");
			Logger.verifyEquals(ECode.BCODE7,
					changesalesrefundinforequestCall.getString("bcode"),
					"验证更新退货退款接口BCode");
			Logger.verifyEquals(
					"退款单不存在:"
							+ changesalesrefundinforequestBean
									.getRefundBillNo(),
					changesalesrefundinforequestCall.getString("msg"),
					"验证更新退货退款接口Msg");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_ChangeSalesRefundInfoRequest_004() {
		Logger.start(false, "申请退款金额大于实际可退款金额");
		try {
			PlaceOrderResp placeOrderResp = PlaceOrderService.placeOrder4();

			int orderId = placeOrderResp.getOrderList().get(0).getOrderId();
			int sellerId = placeOrderResp.getOrderList().get(0).getSellerId();
			int userId = placeOrderResp.getUserId();
			int tradingId = placeOrderResp.getTradingId();

			BigDecimal totalPrice = placeOrderResp.getPayAmount();// 支付金额

			OrderCallService orderCallService = new OrderCallService();

			// 申请退款
			String refundBillNo = orderCallService
					.applySalesRefundRequestInJava(orderId)
					.getSalesRefundInfoList().get(0).getRefundBillNo();
			changesalesrefundinforequestBean.setOrderId(orderId);
			changesalesrefundinforequestBean.setRefundBillNo(refundBillNo);
			changesalesrefundinforequestBean.setRefundAmount(totalPrice
					.add(BigDecimal.TEN));
			changesalesrefundinforequestBean.setRefundProductNum(100);
			changesalesrefundinforequestBean.setRefundType(1);
			changesalesrefundinforequestBean.setReason("123456789");
			changesalesrefundinforequestCall
					.setData(changesalesrefundinforequestBean);
			changesalesrefundinforequestCall.callService();
			Logger.verifyEquals(ECode.FAILED201,
					changesalesrefundinforequestCall.getString("code"),
					"验证更新退货退款接口Code");
			Logger.verifyEquals("114",
					changesalesrefundinforequestCall.getString("bcode"),
					"验证更新退货退款接口BCode");
			Logger.verifyEquals("申请退款金额不能大于实际可退款金额",
					changesalesrefundinforequestCall.getString("msg"),
					"验证更新退货退款接口Msg");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({P3.class})
	@TestCase
	public void Tc_ChangeSalesRefundInfoRequest_005() {
		Logger.start(false, "退款单状态不正确,修改退款单失败");
		try {

			PlaceOrderResp placeOrderResp = PlaceOrderService.placeOrder4();

			int orderId = placeOrderResp.getOrderList().get(0).getOrderId();
			int sellerId = placeOrderResp.getOrderList().get(0).getSellerId();
			int userId = placeOrderResp.getUserId();
			int tradingId = placeOrderResp.getTradingId();

			BigDecimal totalPrice = placeOrderResp.getPayAmount();// 支付金额

			OrderCallService orderCallService = new OrderCallService();

			// 付款
			orderCallService.orderPayRequest(userId, totalPrice, tradingId);
			// 卖家接单
			orderCallService.acceptOrder(orderId, sellerId);
			// 申请退款
			ApplySalesRefundRequestBean applysalesrefundrequestBean = orderCallService
					.applySalesRefundRequestInJava(orderId);
			// 退款完成
			orderCallService
					.salesrefundsuccessrequest(applysalesrefundrequestBean);
			String refundBillNo = applysalesrefundrequestBean
					.getSalesRefundInfoList().get(0).getRefundBillNo();
			changesalesrefundinforequestBean.setOrderId(orderId);
			changesalesrefundinforequestBean.setRefundBillNo(refundBillNo);
			changesalesrefundinforequestBean.setRefundAmount(new BigDecimal(1));
			changesalesrefundinforequestBean.setRefundProductNum(100);
			changesalesrefundinforequestBean.setRefundType(1);
			changesalesrefundinforequestBean.setReason("123456789");
			changesalesrefundinforequestCall
					.setData(changesalesrefundinforequestBean);
			changesalesrefundinforequestCall.callService();
			Logger.verifyEquals(ECode.FAILED201,
					changesalesrefundinforequestCall.getString("code"),
					"验证更新退货退款接口Code");
			Logger.verifyEquals("102",
					changesalesrefundinforequestCall.getString("bcode"),
					"验证更新退货退款接口BCode");
			Logger.verifyEquals(
					"退款单已被审核:"
							+ changesalesrefundinforequestBean
									.getRefundBillNo(),
					changesalesrefundinforequestCall.getString("msg"),
					"验证更新退货退款接口Msg");
		} catch (Exception e) {
			Logger.fail(e);
		}
	}

	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_ChangeSalesRefundInfoRequest_006() {
		Logger.start(true, "更新退款的金额大于商品价格+订单总运费");
		try {

			PlaceOrderResp placeOrderResp = PlaceOrderService.placeOrder7();

			int orderId = placeOrderResp.getOrderList().get(0).getOrderId();
			int userId = placeOrderResp.getUserId();
			int tradingId = placeOrderResp.getTradingId();

			Order order = placeOrderResp.getOrderList().get(0);

			BigDecimal orderFreight = order.getFreight();

			BigDecimal productFreight = order.getOrderInfoList().get(0)
					.getFreight();

			BigDecimal totalPrice = placeOrderResp.getPayAmount();// 支付金额

			OrderCallService orderCallService = new OrderCallService();

			// 付款
			orderCallService.orderPayRequest(userId, totalPrice, tradingId);

			// 申请退款
			String refundBillNo = orderCallService
					.applySalesRefundRequestInJava(orderId)
					.getSalesRefundInfoList().get(0).getRefundBillNo();

			BigDecimal oriRefund = orderCallService
					.applySalesRefundRequestInJava(orderId)
					.getSalesRefundInfoList().get(0).getRefundAmount();

			// 退货退款同步
			changesalesrefundinforequestBean.setOrderId(orderId);
			changesalesrefundinforequestBean.setRefundBillNo(refundBillNo);
			changesalesrefundinforequestBean.setRefundAmount(orderFreight.add(
					oriRefund).add(BigDecimal.ONE));
			changesalesrefundinforequestBean.setRefundProductNum(order
					.getOrderInfoList().get(0).getAmount());
			changesalesrefundinforequestBean.setRefundType(1);
			changesalesrefundinforequestBean.setReason("123456789");
			changesalesrefundinforequestCall
					.setData(changesalesrefundinforequestBean);
			changesalesrefundinforequestCall.callService();

			Logger.verifyEquals(ECode.FAILED201,
					changesalesrefundinforequestCall.getString("code"),
					"验证更新退货退款接口返回正常");

			Logger.verifyEquals(
					String.format("OrderId:%s, 申请退运费金额不能大于实际可退运费金额", orderId),
					changesalesrefundinforequestCall.getString("msg"), "验证msg");

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
	@Test
	@Category({P1.class})
	@TestCase
	public void Tc_ChangeSalesRefundInfoRequest_007() {

		Logger.start(true, "更新商品退款金额加上部分商品运费");
		try {

			PlaceOrderResp placeOrderResp = PlaceOrderService.placeOrder7();

			int orderId = placeOrderResp.getOrderList().get(0).getOrderId();
			int sellerId = placeOrderResp.getOrderList().get(0).getSellerId();
			int userId = placeOrderResp.getUserId();
			int tradingId = placeOrderResp.getTradingId();

			Order order = placeOrderResp.getOrderList().get(0);

			BigDecimal productFreight = order.getOrderInfoList().get(0)
					.getFreight();

			BigDecimal totalPrice = placeOrderResp.getPayAmount();// 支付金额

			OrderCallService orderCallService = new OrderCallService();

			// 付款
			orderCallService.orderPayRequest(userId, totalPrice, tradingId);

			// 申请退款
			String refundBillNo = orderCallService
					.applySalesRefundRequestInJava(orderId)
					.getSalesRefundInfoList().get(0).getRefundBillNo();

			BigDecimal oriRefund = orderCallService
					.applySalesRefundRequestInJava(orderId)
					.getSalesRefundInfoList().get(0).getRefundAmount();

			// 退货退款同步
			changesalesrefundinforequestBean.setOrderId(orderId);
			changesalesrefundinforequestBean.setRefundBillNo(refundBillNo);

			changesalesrefundinforequestBean.setRefundAmount(oriRefund
					.add(BigDecimal.TEN));

			changesalesrefundinforequestBean.setRefundProductNum(order
					.getOrderInfoList().get(0).getAmount());
			changesalesrefundinforequestBean.setRefundType(1);
			changesalesrefundinforequestBean.setReason("123456789");
			changesalesrefundinforequestCall
					.setData(changesalesrefundinforequestBean);
			changesalesrefundinforequestCall.callService();

			Logger.verifyEquals(ECode.SUCCESS,
					changesalesrefundinforequestCall.getString("code"),
					"验证更新退货退款接口返回正常");

			DataUtil.verifyDecimalEquals(BigDecimal.TEN, BigDecimal
					.valueOf((Double) changesalesrefundinforequestCall
							.getData().get("refundFreight")), "验证refundFreight");

			if (changesalesrefundinforequestCall.getString("code")
					.equals("200")) {
				// 数据库验证
				YmtRefundWapper ymtRefund = new YmtRefundWapper();
				Map m = new HashMap();
				m.put("orderId", orderId);
				m.put("refundBillNo", refundBillNo);
				YmtRefundBill refundBill = ymtRefund.selectRefundBill(m).get(0);
				YmtRefundProduct refundPro = ymtRefund.selectRefundProduct(m)
						.get(0);
				Logger.comment("校验Ymt_RefundBill表");
				Logger.verifyEquals(orderId, refundBill.getOrderId(),
						"验证OrderId");
				Logger.verifyEquals(refundBillNo, refundBill.getRefundBillNo(),
						"验证RefundBillNo");
				Logger.verifyEquals(
						changesalesrefundinforequestBean.getRefundAmount(),
						refundBill.getRefundAmount(), "验证RefundAmount");
				Logger.verifyEquals(0, refundBill.getRefundStatus(),
						"验证RefundStatus");
				Logger.verifyEquals(
						changesalesrefundinforequestBean.getRefundAmount(),
						refundBill.getSettlementAmount(), "验证SettlementAmount");
				Logger.verifyEquals(1, refundBill.getSalesRefundStatus(),
						"验证SettlementStatus");
				Logger.verifyEquals(
						changesalesrefundinforequestBean.getRefundType(),
						refundBill.getRefundType(), "验证RefundType");

				Logger.verifyEquals(sellerId, refundBill.getSellerId(),
						"验证SellerId");
				Logger.verifyEquals(userId, refundBill.getBuyerId(),
						"验证BuyerId");
				Logger.verifyEquals(orderId, refundBill.getOrderId(),
						"验证OrderId");
				DataUtil.verifyDecimalEquals(BigDecimal.TEN,
						refundBill.getRefundedAmountOfFreight(),
						"验证RefundedAmountOfFreight");

				Logger.comment("校验Ymt_RefundProduct表");
				Logger.verifyEquals(orderId, refundPro.getOrderId(),
						"验证OrderId");
				Logger.verifyEquals(refundBillNo, refundPro.getRefundBillNo(),
						"验证RefundBillNo");
				Logger.verifyEquals(
						changesalesrefundinforequestBean.getRefundProductNum(),
						refundPro.getRefundProductNum(), "验证RefundProductNum");
			}

		} catch (Exception e) {
			Logger.fail(e);
		}
	}
}
